package erwiki.ui.app.parts.navigation;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.elwiki_data.WikiPage;

public class NavContentProvider implements IStructuredContentProvider, ITreeContentProvider {

	private static final long serialVersionUID = -1954852613672241751L;

	@Override
	public void dispose() {
		// Do nothing
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// Do nothing
	}

	@Override
	public Object[] getChildren(Object parentElement) {
		if (parentElement instanceof WikiPage page) {
			return page.getChildren().toArray();
		}
		return null;
	}

	@Override
	public Object getParent(Object element) {
		if (element instanceof WikiPage page) {
			return page.getParent();
		}
		return null;
	}

	@Override
	public boolean hasChildren(Object element) {
		if (element instanceof WikiPage page) {
			return !page.getChildren().isEmpty();
		}
		return false;
	}

	@Override
	public Object[] getElements(Object inputElement) {
		if (inputElement instanceof WikiPage[]) {
			return (WikiPage[]) inputElement;
		} else if (inputElement instanceof WikiPage page) {
			return new Object[] { page };
		}
		return new Object[0];
	}

}
